/*
 * $Id: api_getncpus.c,v 1.1 2006-02-22 04:51:51 bacon Exp $
 */

#include <xp/sys/api.h>

#if defined(_WIN32)
	#include <pdh.h>
#elif defined(__linux)
	#include <sys/sysinfo.h>
	#include <sys/sysctl.h>
#elif defined(__FreeBSD__)
	#include <kvm.h>
	#include <fcntl.h>
	#include <sys/sysctl.h>
#elif defined(sun)||defined(__sun)
	#include <sys/sysinfo.h>
#else
	#include <sys/sysctl.h>
#endif


int xp_getncpus (xp_size_t* ncpus)
{
#if defined(_WIN32)
	SYSTEM_INFO sys_info;
	GetSystemInfo (&sys_info);
	*ncpus = sys_info.dwNumberOfProcessors;
	return 0;
#elif defined(_SC_NPROCESSORS_CONF)
	long v = sysconf (_SC_NPROCESSORS_CONF);
	if (v == -1) return -1;
	*ncpus = (xp_size_t)v;
	return 0;
#else
	int mib[2] = { CTL_HW, HW_NCPU };
	xp_size_t len = xp_sizeof(*ncpus);
	return sysctl (mib, 2, ncpus, len, NULL, 0);	
#endif
}

